"""
 -*- coding: utf-8-*-
 @File: page_add_member_by_hand.py
 @Author: chen
 @time: 2023/10/04/ 20:37
"""
import allure
from appium.webdriver.common.appiumby import AppiumBy
from appium.webdriver.common.mobileby import MobileBy

from hogwartsHomework.homework_app_auto_L3.dev.pages.page_base import BasePage
from hogwartsHomework.homework_app_auto_L3.utils.mgt_data_save import save_screenshot, save_page_source
from hogwartsHomework.homework_app_auto_L3.utils.mgt_log import logger


class AddMemberByHandPage(BasePage):
    def input_name(self, name="钟离"):
        """输入姓名"""
        textbox_name = (AppiumBy.ID, 'com.tencent.wework:id/c4i')
        with allure.step(f"输入姓名：{name}"):
            logger.info(f"输入姓名：{name}")
            # self.wait_until_visibility(textbox_name)
            self.do_input(textbox_name, name)
        return self

    def input_phone(self, phone_num="13774114734"):
        """输入手机号"""
        textbox_phone = (AppiumBy.ID, 'com.tencent.wework:id/ig9')
        with allure.step(f"输入手机号：{phone_num}"):
            logger.info(f"输入手机号：{phone_num}")
            # self.wait_until_visibility(textbox_phone)
            self.do_input(textbox_phone, phone_num)
        return self

    def click_save(self):
        """点击保存按钮，返回添加成员页面"""
        # self.input_name()
        # self.input_phone()
        button_save = (AppiumBy.ID, 'com.tencent.wework:id/b2r')
        with allure.step("点击保存按钮"):
            logger.info("点击保存按钮")
            # self.wait_until_clickable(button_save)
            self.do_click(button_save)
        # try:
        #     context_success = (MobileBy.XPATH, '//*[contains(@text,"成功")]')
        #     save_screenshot(self.driver)
        #     save_page_source(self.driver)
        #     self.wait_until(context_success)
        #     save_screenshot(self.driver)
        #     save_page_source(self.driver)
        # except Exception as e:
        #     logger.error(e)

        from hogwartsHomework.homework_app_auto_L3.dev.pages.page_add_member import AddMemberPage
        logger.info("进入添加成员页面")
        return AddMemberPage(self.driver)

    def click_save_and_continue(self):
        button_save_continue = (AppiumBy.ID, 'com.tencent.wework:id/b2s')
        with allure.step("点击保存并继续添加按钮"):
            logger.info("点击保存并继续添加按钮")
            # self.wait_until_clickable(button_save_continue)
            self.do_click(button_save_continue)
        return self

    def save_add_member(self, name="钟离", phone_num="13774114734"):
        self.input_name(name)
        self.input_phone(phone_num)
        self.click_save()
        from hogwartsHomework.homework_app_auto_L3.dev.pages.page_add_member import AddMemberPage
        logger.info("进入添加成员页面")
        return AddMemberPage(self.driver)
